﻿using NP.BPMReportPlatform.Utilities;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using Volo.Abp.Domain.Entities;

namespace NP.BPMReportPlatform.Reports.CommonReportConfigs.Dtos
{
    public static class CommonFieldHelper
    {
        public static Type GetTypeByFieldName<T>(string fieldName)
        {
            foreach (var prop in typeof(T).GetProperties())
            {
                if (prop.Name == fieldName)
                {
                    return prop.PropertyType;
                }
            }
            return null;
        }

        public static Type GetCSharpTypeFromDbType(string dbType)
        {
            // 这里只是一个简单的映射示例，实际情况根据实际数据库字段类型进行映射
            switch (dbType.ToUpper())
            {
                case "INT":
                case "BIGINT":
                case "SMALLINT":
                case "TINYINT":
                    return typeof(int);
                case "DECIMAL":
                case "FLOAT":
                case "DOUBLE":
                    return typeof(double);
                case "VARCHAR":
                case "CHAR":
                case "TEXT":
                    return typeof(string);
                case "DATE":
                case "TIME":
                case "DATETIME":
                case "TIMESTAMP":
                    return typeof(DateTime);
                case "BIT":
                    return typeof(bool);
                default:
                    throw new NotSupportedException($"不存在此类型");
            }
        }

        public static object GetTrueValueFromDbType(string dbType, object value, object errorValue, bool enableNull = true)
        {
            // 这里只是一个简单的映射示例，实际情况根据实际数据库字段类型进行映射
            switch (dbType.ToUpper())
            {
                case "INT":
                case "SMALLINT":
                case "TINYINT":
                    return value.ObjToNullInt(errorValue) ?? 0;
                case "BIGINT":
                    return value.ObjToNullLong(errorValue) ?? 0L;
                case "DECIMAL":
                case "FLOAT":
                case "DOUBLE":
                    return value.ObjToNullDecimal(errorValue) ?? 0;
                case "VARCHAR":
                case "CHAR":
                case "TEXT":
                    return value.ObjToString() ?? string.Empty;
                case "DATE":
                case "TIME":
                case "DATETIME":
                case "TIMESTAMP":
                    return value.ObjToNullDate(errorValue) ?? DateTime.MinValue;
                case "BIT":
                    return value.ObjToNullBool(errorValue) ?? false;
                default:
                    throw new NotSupportedException($"不存在此类型");
            }
        }

        /// <summary>
        /// 获取默认值
        /// </summary>
        /// <returns></returns>
        public static object GetDefaultValue(int defaultType, string defaultValue, string dbType, object dbDefaultValue, Entities.BPM_Platform.Users user,
            string dateFormat = "yyyy-MM-dd HH:mm:ss")
        {
            switch (defaultType)
            {
                case 1:
                    var currentTime = DateTime.Now.ToString(string.IsNullOrWhiteSpace(dateFormat) ? "yyyy-MM-dd HH:mm:ss" : dateFormat);
                    return GetTrueValueFromDbType(dbType, currentTime, dbDefaultValue);
                case 2:
                    return GetTrueValueFromDbType(dbType, user?.UserId ?? null, dbDefaultValue);
                case 3:
                    return GetTrueValueFromDbType(dbType, user?.WorkNumber ?? null, dbDefaultValue);
                case 4:
                    return GetTrueValueFromDbType(dbType, user?.UserName ?? null, dbDefaultValue);
                case 5:
                    return GetTrueValueFromDbType(dbType, Guid.NewGuid().ToString(), dbDefaultValue);
                case 1000:
                    return GetTrueValueFromDbType(dbType, defaultValue, dbDefaultValue);
                default:
                    return GetTrueValueFromDbType(dbType, dbDefaultValue, null);
            }
        }
    }
}
